package edu.xpu.project.common.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @ClassName: ConfigReader.java
 * @Description: 读取系统中配置文件
 *
 * @author liukang
 * @date 2017年4月19日 上午10:19:31
 * @version 
 * @since JDK 1.6
 */
public class ConfigReader {
	
	private static Logger log = LoggerFactory.getLogger(ConfigReader.class);
	
	public static String sysPropertiesVal(String key) {
		Properties properties = getProperties("sys.properties");
		return properties.getProperty(key);
	} 
	
	private static Properties getProperties(String fileName) {
		if (fileName == null || "".equals(fileName)) {
			log.debug(fileName + " 文件为null或者为空串.");
			return null;
		}
		String absolutePath = new File("").getAbsolutePath();
		String propertiesFilePath = absolutePath + "\\src\\main\\resources\\properties";
		File path = new File(propertiesFilePath);
		File[] listFiles = path.listFiles();
		for (int i = 0, len = listFiles.length; i < len; i++) {
			File file = listFiles[i];
			String name = file.getName();
			if (fileName.equals(name)) {
				Properties properties = new Properties();
				FileInputStream inputStream = null;
				try {
					inputStream = new FileInputStream(file);
					properties.load(inputStream);
				} catch (FileNotFoundException e) {
					log.error("配置文件没有发现." + e);
				} catch (IOException e) {
					log.error("配置文件加载异常." + e);
				}
				return properties;
			}
		}
		return null;
	}
	
	public static void main(String[] args) {
		getProperties("");
	}

}

